package com.yoomiito.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.listener.OnViewClickListener;
import com.yoomiito.app.utils.AppManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/************************************************************
 * ,* Author:  linqiang
 * ,* Description:  自定义了一条title
 ************************************************************/
public class TitleView extends LinearLayout {
    @BindView(R.id.iv_back_left)
    ImageView backIv;
    @BindView(R.id.tv_back_left)
    TextView backTv;
    @BindView(R.id.tv_center)
    TextView titleTv;
    @BindView(R.id.tv_right)
    TextView rightTv;
    @BindView(R.id.iv_right)
    ImageView rightIv;

    private Context mContext;

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_title,
                null, false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, view);
        this.addView(view, params);

        TypedArray typedArray = mContext.obtainStyledAttributes(attrs,
                R.styleable.TitleView, 0, 0);

        for (int i = 0; i < typedArray.getIndexCount(); i++) {

            int attr = typedArray.getIndex(i);

            switch (attr) {
                /**
                 * 设置标题
                 */
                case R.styleable.TitleView_tvCenterText:
                    String title = typedArray.getString(attr);
                    titleTv.setText(title);
                    break;
                case R.styleable.TitleView_tvRightText:
                    String rightText = typedArray.getString(attr);
                    rightTv.setText(rightText);
                    break;
                /**
                 * 设置左边图片
                 */
                case R.styleable.TitleView_tvleftImageVisible:
                    boolean leftIvVisible = typedArray.getBoolean(attr, true);
                    if (leftIvVisible) {
                        backIv.setVisibility(VISIBLE);
                    } else {
                        backIv.setVisibility(GONE);
                    }
                    break;
                /**
                 * 设置右边文字显示与否
                 */
                case R.styleable.TitleView_tvrightTvVisibility:
                    boolean rightTvVisible = typedArray.getBoolean(attr, true);
                    if (rightTvVisible) {
                        rightTv.setVisibility(VISIBLE);
                    } else {
                        rightTv.setVisibility(GONE);
                    }
                    break;
                /**
                 * 设置左边图片
                 */
                case R.styleable.TitleView_tvleftImageView:
                    int leftResourceId = typedArray.getResourceId(attr, 0);
                    backIv.setImageResource(leftResourceId);
                    break;
                /**
                 * 设置右边图片
                 */
                case R.styleable.TitleView_tvrightImageView:
                    int rightResourceId = typedArray.getResourceId(attr, 0);
                    rightIv.setImageResource(rightResourceId);
                    rightIv.setVisibility(VISIBLE);
                    break;
                /**
                 * 设置右边文字颜色
                 * */
                case R.styleable.TitleView_tvRightColor:
                    rightTv.setTextColor(typedArray.getColor(attr, 0x000000));
                    break;
                /**
                 * 设置中间文字颜色
                 * */
                case R.styleable.TitleView_tvTitleColor:
                    titleTv.setTextColor(typedArray.getColor(attr, 0x000000));
                    break;
            }
        }
        typedArray.recycle();
    }

    @OnClick({R.id.iv_back_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_left:
                /*
                    这里一定概率报错
                    NoSuchElementException
                 */
                AppManager.getAppManager().finishActivity();
                break;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    private OnViewClickListener rightClickListener;

    public void setOnRightClick(final OnViewClickListener rightClickListener) {
        this.rightClickListener = rightClickListener;
        rightTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                rightClickListener.onClick(view);
            }
        });

        rightIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                rightClickListener.onClick(view);
            }
        });
    }

    public void setCenterText(String centerText) {
        titleTv.setText(centerText);
    }

    public void setRightTv(String rightText) {
        rightTv.setText(rightText);
    }

    public void setTitleStyle() {
        TextPaint tp = titleTv.getPaint();
        tp.setFakeBoldText(true);
    }

    public void setRightTvVisibility(int visibility) {
        rightTv.setVisibility(visibility);
    }

}
